﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sebrae.Academico.Trilhas.Dominio.Classes;
using Sebrae.Academico.Trilhas.BP;

namespace Sebrae.Academico.Trilhas.WebForms.Cadastros
{
    public partial class EdicaoTrilha : System.Web.UI.Page
    {

        private Sebrae.Academico.Trilhas.Dominio.Classes.Trilha trilhaEdicao = null;
        private ManterTrilha manterTrilha = new ManterTrilha();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Session["TrilhaEdit"] != null)
            {
                int idtrilha = int.Parse(Session["TrilhaEdit"].ToString());
                trilhaEdicao = manterTrilha.ObterTrilhaporId(idtrilha);
            }
            else
                trilhaEdicao = new Trilha();

            txtFiltroNome.Text = trilhaEdicao.Nome;

        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Session.Remove("TrilhaEdit");
            Response.Redirect("ListaTrilha.aspx");
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            trilhaEdicao = new Trilha();
            trilhaEdicao.Nome = txtFiltroNome.Text;

            if (Session["TrilhaEdit"] == null)
            {
               
                manterTrilha.IncluirTrilha(trilhaEdicao);
                trilhaEdicao = new Dominio.Classes.Trilha();
                txtFiltroNome.Text = "";
            }
            else
            {
                trilhaEdicao.ID = int.Parse(Session["TrilhaEdit"].ToString()); 
                manterTrilha.AlterarTrilha(trilhaEdicao);
                Session.Remove("TrilhaEdit");
                Response.Redirect("ListaTrilha.aspx");
            }
            
        }
    }
}